package com.xiaoyu.cloudmusic.service;

import com.xiaoyu.cloudmusic.dao.PlaylistMapper;
import com.xiaoyu.cloudmusic.pojo.*;
import com.xiaoyu.cloudmusic.resp.PlayList;
import com.xiaoyu.cloudmusic.utils.CommentUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service
public class PlayListServiceImpl implements PlayListService{
    @Autowired
    PlaylistMapper playlistMapper;
    @Override
    public List<Recommend> getRandRecommend(int num) {
        return playlistMapper.getRandRecommend(num);
    }

    @Override
    public PlayListDetail getPlayListDetail(int id) {
        return playlistMapper.getPlayListDetail(id);
    }

    @Override
    public List<Song> getPlayListSong(int id) {
        return playlistMapper.getPlayListSong(id);
    }

    @Override
    public PlayListDetail getLastPlayListDetail() {
        return playlistMapper.getLastPlayListDetail();
    }

    @Override
    public int addPlayList(String uuid, int id, String title, String createTime,String isPublic) {
        return playlistMapper.addPlayList(uuid, id, title, createTime,isPublic);
    }

    @Override
    public int updateMyPlayList(PlayListDetail playListDetail) {
        return playlistMapper.updateMyPlayList(playListDetail);
    }

    @Override
    public int updateCover(String coverImgUrl, String listId) {
        return playlistMapper.updateCover(coverImgUrl, listId);
    }


    @Override
    public int deletePlaylistById(int id) {
        return playlistMapper.deletePlaylistById(id);
    }





    @Override
    public int delPlayListTracksByListId(int listId) {
        return playlistMapper.delPlayListTracksByListId(listId);
    }

    @Override
    public int addSongByID(int musicId, int playlistId) {
        return playlistMapper.addSongByID(musicId, playlistId);
    }

    @Override
    public int deleteSongByID(int musicId, int playlistId) {
        return playlistMapper.deleteSongByID(musicId, playlistId);
    }

    @Override
    public Playlisttrack findTrackInList(int musicId, int playlistId) {
        return playlistMapper.findTrackInList(musicId, playlistId);
    }

    @Override
    public int addRecommend(Recommend recommend) {
        return playlistMapper.addRecommend(recommend);
    }

    @Override
    public int updateRecommend(Recommend recommend) {
        return playlistMapper.updateRecommend(recommend);
    }

    @Override
    public void delRecommend(int id) {
        playlistMapper.delRecommend(id);
    }

    @Override
    public int addComment(CommentInfo commentInfo) {
        return playlistMapper.addComment(commentInfo);
    }

    @Override
    public int addReply(CommentInfo commentInfo) {
        return playlistMapper.addReply(commentInfo);
    }

    @Override
    public List<CommentInfo> getAllComment(String uuid,int listId) {
        List<CommentInfo> firstCommentInfoList = playlistMapper.getFirstComment(listId);
        List<CommentInfo> replyList = playlistMapper.getReply(listId);
        List<Integer> ids = playlistMapper.getIds(uuid);
        CommentUtil.encapsulateSupport(ids, firstCommentInfoList);
        CommentUtil.encapsulateSupport(ids, replyList);
        return CommentUtil.getAllComment(firstCommentInfoList,replyList);
    }

    @Override
    public int delComment(int id) {
        return playlistMapper.delComment(id);
    }

    @Override
    public boolean support(int id, int isSupport,String uuid,int commentid) {
        if(isSupport == 1){
            playlistMapper.addSupport(uuid,commentid);
            playlistMapper.supportComment(id);
            return true;
        }
        playlistMapper.delSupport(uuid,commentid);
        playlistMapper.noSupportComment(id);
        return false;
    }

    @Override
    public List<PlayList> getUserPublicList(String uuid) {
        return playlistMapper.getUserPublicList(uuid);
    }

    @Override
    public int addFavorites(String uuid,Integer listid) {
        return playlistMapper.addFavorites(uuid,listid);
    }

    @Override
    public int removeFavorites(String uuid,Integer listid) {
        return playlistMapper.removeFavorites(uuid,listid);
    }

    @Override
    public List<PlayList> getUserFavorites(String uuid) {
        return playlistMapper.getUserFavorites(uuid);
    }

    @Override
    public boolean isFavorites(String uuid, int id) {
        return playlistMapper.getFavoritesIds(uuid).contains(id);
    }
//    @Override
//    public CommentInfo getComment(String id) {
//        return PlaylistMapper.getComment(id);
//    }
//
//    @Override
//    public List<CommentInfo> getAllComment(int listId) {
//        return PlaylistMapper.getAllComment(listId);
//    }



//    @Override
//    public List<ReplyInfo> getAllReply(String id) {
//        return PlaylistMapper.getAllReply(id);
//    }
//
//    @Override
//    public int delCommentOrReply(CommentParam commentParam) {
//        int res1 = PlaylistMapper.delComment(commentParam);
//        int res2 = PlaylistMapper.delReply(commentParam);
//        return Math.max(res1, res2);
//    }
//
//    @Override
//    public int support(CommentParam commentParam) {
//        return Math.max(PlaylistMapper.supportComment(commentParam),PlaylistMapper.supportReply(commentParam));
//    }
//
//    @Override
//    public int noSupport(CommentParam commentParam) {
//        return Math.max(PlaylistMapper.noSupportComment(commentParam),PlaylistMapper.noSupportReply(commentParam));
//    }

}
